// --------------------------------------------------------------------------------------------------------------------
// <copyright file="MockServiceLocator.cs" company="Concept Architecture">
//   (c) 2010 Muhammad Umer Farooq
// </copyright>
// <summary>
//   Defines the MockServiceLocator type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Hisaab.Client.Desktop.Tests
{
    using System;
    using System.Collections.Generic;

    using Microsoft.Practices.ServiceLocation;

    using Moq;

    /// <summary>
    /// Defines the <seealso cref="MockServiceLocator"/> type.
    /// </summary>
    public class MockServiceLocator : ServiceLocatorImplBase
    {
        /// <summary>
        /// Member variable _mockServiceLocator
        /// </summary>
        private readonly Mock<IServiceLocator> _mockServiceLocator = new Mock<IServiceLocator>();

        /// <summary>
        /// Initializes a new instance of the <see cref="MockServiceLocator"/> class.
        /// </summary>
        public MockServiceLocator()
        {
            ServiceLocator.SetLocatorProvider(() => this);
        }

        /// <summary>
        /// Sets the injection.
        /// </summary>
        /// <typeparam name="T">Type of Mock</typeparam>
        /// <param name="mockObject">The mock.</param>
        public void SetInjection<T>(T mockObject) where T : class
        {
            _mockServiceLocator.Setup(x => x.GetInstance(typeof(T))).Returns(mockObject);
        }

        #region Overrides of ServiceLocatorImplBase

        /// <summary>
        /// Gets the instance.
        /// </summary>
        /// <typeparam name="TService">The type of the service.</typeparam>
        /// <returns>Service object requested</returns>
        public override TService GetInstance<TService>()
        {
            return (TService)_mockServiceLocator.Object.GetInstance(typeof(TService));
        }

        /// <summary>
        /// Does the get instance.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <param name="key">The key.</param>
        /// <returns>Service object requested</returns>
        protected override object DoGetInstance(Type serviceType, string key)
        {
            return _mockServiceLocator.Object.GetInstance(serviceType, key);
        }

        /// <summary>
        /// Does the get all instances.
        /// </summary>
        /// <param name="serviceType">Type of the service.</param>
        /// <returns>Service object requested</returns>
        protected override IEnumerable<object> DoGetAllInstances(Type serviceType)
        {
            return _mockServiceLocator.Object.GetAllInstances(serviceType);
        }

        #endregion Overrides of ServiceLocatorImplBase
    }
}